package com.wdk.processor;

import com.squareup.javapoet.ParameterSpec;
import com.squareup.javapoet.TypeName;

import java.util.ArrayList;
import java.util.List;

import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Name;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.TypeMirror;


public class MtopSendField {
    private ExecutableElement mVariableElement;
    private String api;
    private String version;

    private String dataType;

    public MtopSendField(Element element) {
        //排错
        if (element.getKind() != ElementKind.METHOD) {
            throw new IllegalArgumentException(String.format("Only fields can be annotated with @%s",
                    RequestFit.class.getSimpleName()));
        }
        System.out.println("MtopSendField before mVariableElement: " + element);
        mVariableElement = (ExecutableElement) element;
        System.out.println("MtopSendField after mVariableElement: " + mVariableElement);
        //获取自定义注解
        RequestFit requestFit = mVariableElement.getAnnotation(RequestFit.class);

        RequestDataType requestDataType = mVariableElement.getAnnotation(RequestDataType.class);
        if(null !=requestDataType) {
            dataType = requestDataType.type();
        }

        //拿到控件的id
        api = requestFit.api();
        version = requestFit.version();
        System.out.println("BindViewField mResId: " + api);
        if (null == api) {
            throw new IllegalArgumentException(
                    String.format("value() in %s for field %s is not valid !", RequestFit.class.getSimpleName(),
                            mVariableElement.getSimpleName()));
        }
    }

    public String getDataType() {
        return dataType;
    }

    public Name getMethodName() {
        return mVariableElement.getSimpleName();
    }

    public ExecutableElement getmVariableElement() {
        return mVariableElement;
    }

    public List<ParameterSpec> getMethodParams() {
//        try {

        List<ParameterSpec> specList = new ArrayList<>();

            for (VariableElement element: mVariableElement.getParameters()) {
                specList.add(ParameterSpec.builder(TypeName.get(element.asType()), element.getSimpleName().toString())
                        .addModifiers(element.getModifiers())
                        .build());
            }

//            ClassPool.getDefault().insertClassPath(new ClassClassPath(NetRequest.class));
//            //获取要操作的类对象
//            ClassPool pool = ClassPool.getDefault();
//            CtClass ctClass = pool.get("com.test.javapoettest.NetRequest");
//
//            //获取要操作的方法参数类型数组，为获取该方法代表的CtMethod做准备
//            Method method = NetRequest.class.getMethod(mVariableElement.getSimpleName().toString(), String.class);
//            int count = method.getParameterCount();
//            Class<?>[] paramTypes = method.getParameterTypes();
//            CtClass[] ctParams = new CtClass[count];
//            for (int i = 0; i < count; i++) {
//                ctParams[i] = pool.getCtClass(paramTypes[i].getName());
//            }
//
//            CtMethod ctMethod = ctClass.getDeclaredMethod(mVariableElement.getSimpleName().toString(), ctParams);
//            //得到该方法信息类
//            MethodInfo methodInfo = ctMethod.getMethodInfo();
//
//            //获取属性变量相关
//            CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
//            System.out.println("打印CodeAttribute: " + codeAttribute);
//            //获取方法本地变量信息，包括方法声明和方法体内的变量
//            //需注意，若方法为非静态方法，则第一个变量名为this

//            if (codeAttribute != null) {
//            LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag);
//            int pos = javassist.Modifier.isAbstract(method.getModifiers()) ? 0 : 1;
//                for (int i = 0; i < count; i++) {
//                    specList.add(ParameterSpec.builder(TypeName.OBJECT, attr.variableName(i + pos)).build());
//
//                }
//            }

//            System.out.println("打印方法: " + specList.toString());

            return specList;

//        } catch (NoSuchMethodException e) {
//            e.printStackTrace();
//        } catch (NotFoundException e) {
//            e.printStackTrace();
//        }

//        return new ArrayList<>();
    }

    public String getVersion() {
        return version;
    }

    public String getApi() {
        return api;
    }

    /**
     * 获取变量类型
     *
     * @return
     */
    TypeMirror getFieldType() {
        return mVariableElement.asType();
    }
}